package com.csi.service.impl;

import com.csi.domain.*;
import com.csi.mapper.HotelMapper;
import com.csi.service.HotelService;
import com.csi.utils.ReturnResults;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    //查找全部(标志前缀)
    private static final String FIND_ALL_KEY_PREFIX = "findAll:";
    //推荐酒店
    private static final String RECOMMEND_KEY_PREFIX = "recommendHotel:";
    //低价酒店
    private static final String DISCOUNT_FIND_ALL_KEY_PREFIX = "discount:";



    /**
     * 新增酒店 -酒店表-酒店位置表-审核表
     * @param hotel 酒店对象(里面封装了酒店位置对象)
     * @return 执行结果
     */
    @Override
    @Transactional
    public int save(Hotel hotel) {
        int saved =  hotelMapper.save(hotel);
        hotel.getHotelLocation().setHotelId(hotel.getId());
        hotel.getHotelLocation().setDetailLocation(hotel.getHotelAddress());
        int saveLocation =  hotelMapper.saveLocation(hotel.getHotelLocation());
        int audited = hotelMapper.saveHotelAudit(hotel.getId(), ReturnResults.WAIT_AUDIT_STATUS);
        return saveLocation + audited + saved;

    }

    /**
     * 新增酒店加盟信息 酒店加盟-审核表
     * @param hotelFranchise 酒店加盟
     * @return 执行条数
     */
    @Override
    @Transactional
    public int saveHotelFranchise(HotelFranchise hotelFranchise) {
        int saved = hotelMapper.saveHotelFranchise(hotelFranchise);
        int audited = hotelMapper.saveFranchiseAudit(hotelFranchise.getId(),ReturnResults.WAIT_AUDIT_STATUS);
        return audited + saved;
    }

    /**
     * 查找全部酒店
     * @return
     */
    @Override
    public List<Hotel> findAll() {
        List<Hotel> hotelList  = (List<Hotel>)redisTemplate.opsForValue().get(FIND_ALL_KEY_PREFIX);

        if (hotelList == null){
            hotelList = hotelMapper.findAll();
            if (hotelList != null && hotelList.size()!=0){
                redisTemplate.opsForValue().set(FIND_ALL_KEY_PREFIX,hotelList);
                System.out.println("=====================全部酒店未放入缓存");
                return hotelList;
            }
        }else {
            System.out.println("=====================全部酒店已在缓存");
            return hotelList;
        }
        return hotelList;
    }

    /**
     * 推荐酒店
     * @return
     */
    @Override
    public List<Hotel> recommendHotel() {
        redisTemplate.opsForValue().set(RECOMMEND_KEY_PREFIX,null);
        List<Hotel> hotelList  = (List<Hotel>)redisTemplate.opsForValue().get(RECOMMEND_KEY_PREFIX);

        if (hotelList == null){
            hotelList = hotelMapper.recommendHotel();
            if (hotelList != null && hotelList.size()!=0){
                redisTemplate.opsForValue().set(RECOMMEND_KEY_PREFIX,hotelList);
                System.out.println("=====================全部酒店未放入缓存");
                return hotelList;
            }
        }else {
            System.out.println("=====================全部酒店已在缓存");
            return hotelList;
        }
        return hotelList;
    }

    /**
     * 特价酒店
     * @return
     */
    @Override
    public List<Hotel> discountedhotel() {
        List<Hotel> hotelList  = (List<Hotel>)redisTemplate.opsForValue().get(DISCOUNT_FIND_ALL_KEY_PREFIX);

        if (hotelList == null){
            hotelList = hotelMapper.discountedhotel();
            if (hotelList != null && hotelList.size()!=0){
                redisTemplate.opsForValue().set(DISCOUNT_FIND_ALL_KEY_PREFIX,hotelList);
                System.out.println("=====================低价酒店未放入缓存");
                return hotelList;
            }
        }else {
            System.out.println("=====================低价酒店已在缓存");
            return hotelList;
        }
        return hotelList;
    }

    /**
     * 查找所有酒店名
     * @return 执行结果
     */
    @Override
    public List<Hotel> findAllHotelName() {
        return hotelMapper.findAllHotelName();
    }

    /**
     * 根据用户名字查找预定过的酒店
     * @param userId 用户id
     * @return 执行结果
     */
    @Override
    public List<Map<String, Object>>  findHotelByUserId(long userId) {
        return hotelMapper.findHotelByUserId(userId);
    }

    /**
     * 根据用户名字查找未支付的酒店
     * @param userId 用户id
     * @return 执行结果
     */
    @Override
    public List<Map<String, Object>>  findHotelByUserIdUnpaid(long userId) {
        return hotelMapper.findHotelByUserIdUnpaid(userId);
    }

    /**
     * 根据用户名字查找未入住的酒店
     * @param userId 用户id
     * @return 执行结果
     */
    @Override
    public List<Map<String, Object>>  findHotelByIdTravel(long userId) {

        return hotelMapper.findHotelByIdTravel(userId);
    }

    /**
     * 根据用户名字查找评价
     * @param userId 用户id
     * @return 执行结果
     */
    @Override
    public List<Map<String, Object>>  findHotelByIdAssess(long userId) {

        return hotelMapper.findHotelByIdAssess(userId);
    }

    /**
     * 根据酒店id查找酒店名字
     * @param hotelId 酒店id
     * @return 执行结果
     */
    @Override
    public String findHotelName(long hotelId) {
        return hotelMapper.findHotelName(hotelId);
    }

    /**
     * 根据酒店地址信息模糊查询
     * @param name 酒店地址信息部分字段
     * @return
     */
    @Override
    public List<Hotel> likeHotelByAddress(String name) {
        return hotelMapper.likeHotelByAddress(name);
    }


    /**
     * 根据城市名称、入住日、离店日、入住人数模糊查询
     * @param peopleNum 入住人数
     * @param city_location 城市名称
     * @param check_time 入住日
     * @param leave_time 离店日
     * @return 返回有空闲房间的酒店信息
     */
    @Override
    public List<Hotel> like4book(Integer peopleNum, String city_location, String check_time, String leave_time) {
        return hotelMapper.like4book(peopleNum, city_location, check_time, leave_time);
    }

    /**
     * 查询酒店的起步价、评论数量、房型数量
     * @param hotelId
     * @return
     */
    @Override
    public Map<String, Object> findHotelById(long hotelId) {
        return hotelMapper.findHotelById(hotelId);
    }


    /**
     * 查询酒店详情页面内容
     * @param hotelId 酒店id
     * @return Map<String,Object>
     */
    @Override
    public Map<String, Object> findHotelListById(long hotelId) {
        Map<String,Object> hotelBase =  hotelMapper.findHotelListById(hotelId);

        if (hotelBase != null){
            List<FacilityInfo> hotelFacilities = hotelMapper.findHotelFacilities(hotelId);
            hotelBase.put("facilities",hotelFacilities);
        }
        return hotelBase;
    }

    /**
     * 查询房间类型
     * @param hotelId 酒店id
     * @param checkTime 入住时间
     * @param leaveTime 离店时间
     * @return Map<String,Object>
     */
    @Override
    public List<Map<String, Object>> findRoomTypeById(long hotelId, String checkTime, String leaveTime) {
        List<Map<String, Object>> roomType = hotelMapper.findRoomTypeById(hotelId,checkTime,leaveTime);

        for (Map<String, Object> m :roomType){
            List<FacilityInfo> roomFacilitiesById = hotelMapper.findRoomFacilitiesById((Long) m.get("id"));
            m.put("facilities",roomFacilitiesById);
        }
        return roomType;
    }


    /**
     * 查询房间类型评价
     * @param hotelId 酒店id
     * @return Map<String, Object>
     */
    @Override
    public List<Map<String, Object>>  findAssessById(long hotelId) {
        return  hotelMapper.findAssessById(hotelId);
    }

}
